<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ConvertFormatUtil
 *
 * @author Sorasak Siangsanan
 */
class ConvertFormatUtil {

    public static function getStringDateWithoutTime($date) {
        if (!isset($date)) {
            return date('h:i');
        }
        try {
            $strDateTime = explode(' ', trim($date));
            return $strDateTime[0];
        } catch (Exception $ex) {
            return date('h:i');
        }
    }

    public static function getTimeOfDate($date, $isWithoutMilisecond) {
        if (!isset($date)) {
            return date('h:i');
        }
        try {
            $strDateTime = explode(' ', trim($date));
            $strTime = explode(':', $strDateTime[1]);
            if ($isWithoutMilisecond) {
                return $strTime[0] . ':' . $strTime[1] . ':' . $strTime[2];
            } else {
                return $strTime[0] . ':' . $strTime[1];
            }
        } catch (Exception $ex) {
            return date('h:i');
        }
    }

    public static function BEStringToADString($date, $isWithoutTime) {
        if (!isset($date)) {
            return date('Y-m-d h:i:s');
        }
        try {
            $strDateTime = explode(' ', trim($date));
            $strDate = explode('-', $strDateTime[0]);
            if ($isWithoutTime) {
                return ($strDate[0] - 543) . '-' . $strDate[1] . '-' . $strDate[2] . ' ' . $strDateTime[1];
            } else {
                return ($strDate[0] - 543) . '-' . $strDate[1] . '-' . $strDate[2];
            }
        } catch (Exception $ex) {
            return date('Y-m-d h:i:s');
        }
    }

    public static function ADStringToBEString($date, $isWithoutTime) {
        if (!isset($date)) {
            return date('Y-m-d h:i:s');
        }
        try {
            $strDateTime = explode(' ', trim($date));
            $strDate = explode('-', $strDateTime[0]);
            if ($isWithoutTime) {
                return ($strDate[0] + 543) . '-' . $strDate[1] . '-' . $strDate[2] . ' ' . $strDateTime[1];
            } else {
                return ($strDate[0] + 543) . '-' . $strDate[1] . '-' . $strDate[2];
            }
        } catch (Exception $ex) {
            return date('Y-m-d h:i:s');
        }
    }

    public static function stringToDate($strDate, $format) {
        try {
            return date($format, strtotime($strDate));
        } catch (Exception $ex) {
            return date($format);
        }
    }

    public static function getDateLocale($strDate, $locale) {
        if ($locale == 'th') {
            return ConvertFormatUtil::getThaiDate(date("Y-m-d H:i:s", strtotime($strDate)));
        } else {
            return date("j M Y, H:i:s", strtotime($strDate));
        }
    }

    public static function getThaiDate($strDate) {

        $strYear = date("Y", strtotime($strDate)) + 543;
        $strMonth = date("n", strtotime($strDate));
        $strDay = date("j", strtotime($strDate));
        $strHour = date("H", strtotime($strDate));
        $strMinute = date("i", strtotime($strDate));
        $strSeconds = date("s", strtotime($strDate));
        $strMonthCut = Array("", "ม.ค.", "ก.พ.", "มี.ค.", "เม.ย.", "พ.ค.", "มิ.ย.", "ก.ค.", "ส.ค.", "ก.ย.", "ต.ค.", "พ.ย.", "ธ.ค.");
        $strMonthThai = $strMonthCut[$strMonth];

        if (Yii::app()->language != 'th') {
            if ($strHour == 0 && $strMinute == 0 && $strSeconds == 0) {
                return date("d M, y", strtotime($strDate));
            } else {
                return date("d M y, H:i:s", strtotime($strDate));
            }
        }

        if ($strHour == 0 && $strMinute == 0 && $strSeconds == 0) {
            return "$strDay $strMonthThai $strYear";
        } else {
            return "$strDay $strMonthThai $strYear, $strHour:$strMinute:$strSeconds";
        }
    }
    
    public static function getThaiDateForMaster($strDate) {

        $strYear = date("Y", strtotime($strDate)) + 543;
        $strMonth = date("n", strtotime($strDate));
        $strDay = date("j", strtotime($strDate));
        $strDayOfWeek = date("l", strtotime($strDate));
        $strHour = date("H", strtotime($strDate));
        $strMinute = date("i", strtotime($strDate));
        $strSeconds = date("s", strtotime($strDate));
        $strMonthCut = Array("", "ม.ค.", "ก.พ.", "มี.ค.", "เม.ย.", "พ.ค.", "มิ.ย.", "ก.ค.", "ส.ค.", "ก.ย.", "ต.ค.", "พ.ย.", "ธ.ค.");
        $strMonthThai = $strMonthCut[$strMonth];

        if (Yii::app()->language != 'th') {
            if ($strHour == 0 && $strMinute == 0 && $strSeconds == 0) {
                return date("d M, y", strtotime($strDate));
            } else {
                return date("d M y, H:i:s", strtotime($strDate));
            }
        }

        if ($strHour == 0 && $strMinute == 0 && $strSeconds == 0) {
            return "($strDayOfWeek) $strDay $strMonthThai $strYear";
        } else {
            return "($strDayOfWeek) $strDay $strMonthThai $strYear, $strHour:$strMinute:$strSeconds";
        }
    }

    public static function getFullGender($shortGender) {
        if ($shortGender == 1) {
            return Yii::t('register', 'male');
        } else if ($shortGender == 2) {
            return Yii::t('register', 'female');
        } else {
            return '';
        }
    }

}
